package com.easydev.mq.rabbit;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.easydev.mq.MqConfig;

public class RabbitMQConfig extends MqConfig {
    private final static Logger LOGGER = LoggerFactory.getLogger(RabbitMQConfig.class);

    private static final String R_HOST = "rabbit.host";

    private static final String R_PORT = "rabbit.port";

    private static final String R_USERNAME = "rabbit.username";

    private static final String R_PASSWORD = "rabbit.password";

    private static final String R_CONNECTION_TIMEOUT = "rabbit.virtualhost";

    private static final String R_AUTOACK = "rabbit.autoack";

    private static final String R_DISTRIBUTION = "rabbit.distribution";

    private static final String R_EXCEPTION = "rabbit.exception";

    private static final String R_CONNECTION_NUM = "rabbit.connection.num";

    private static final String R_CHANNEL_PER_CONNECTION = "rabbit.channel.pre.connection";

    private static final String R_VIRTUALHOST_DEFAULT = "rabbit.defautvirtualhost.defaut";

    private static final String R_VIRTUALHOSTS = "rabbit.virtualhosts";

    private static final String R_THREAD_POOL = "rabbit.threadPool";

    public static String HOST;

    public static String THREAD_POOL;

    public static String PORT = "5672";

    public static String USERNAME;

    public static String PASSWORD;

    public static String VIRTUALHOST;

    public static String CONNECTIONTIMEOUT;

    public static String AUTOACK;

    public static String DISTRIBUTION;

    public static String EXCEPTION;

    public static int CONNECTION_NUM = 3;

    public static int CHANNEL_PER_CONNECTION = 20;

    public static int POOL_NUM;

    public static String VIRTUALHOST_DEFAULT;

    public static String VIRTUALHOSTS;

    static {
        LOGGER.debug("init rabbitMq connection config");
        String tempProperty = null;
        if (StringUtils.isNotEmpty(tempProperty = config.getProperty(R_HOST))) {
            HOST = tempProperty;
        }
        if (StringUtils.isNotEmpty(tempProperty = config.getProperty(R_PORT))) {
            PORT = tempProperty;
        }
        if (StringUtils.isNotEmpty(tempProperty = config.getProperty(R_USERNAME))) {
            USERNAME = tempProperty;
        }
        if (StringUtils.isNotEmpty(tempProperty = config.getProperty(R_PASSWORD))) {
            PASSWORD = tempProperty;
        }
        if (StringUtils.isNotEmpty(tempProperty = config.getProperty(R_CONNECTION_TIMEOUT))) {
            CONNECTIONTIMEOUT = tempProperty;
        }
        if (StringUtils.isNotEmpty(tempProperty = config.getProperty(R_AUTOACK))) {
            AUTOACK = tempProperty;
        }
        if (StringUtils.isNotEmpty(tempProperty = config.getProperty(R_DISTRIBUTION))) {
            DISTRIBUTION = tempProperty;
        }
        if (StringUtils.isNotEmpty(tempProperty = config.getProperty(R_EXCEPTION))) {
            EXCEPTION = tempProperty;
        }

        if (StringUtils.isNotEmpty(tempProperty = config.getProperty(R_CONNECTION_NUM))) {
            CONNECTION_NUM = Integer.parseInt(tempProperty);
        }
        if (StringUtils.isNotEmpty(tempProperty = config.getProperty(R_CHANNEL_PER_CONNECTION))) {
            CHANNEL_PER_CONNECTION = Integer.parseInt(tempProperty);
        }
        if (StringUtils.isNotEmpty(tempProperty = config.getProperty(R_CHANNEL_PER_CONNECTION))) {
            CHANNEL_PER_CONNECTION = Integer.parseInt(tempProperty);
        }
        if (StringUtils.isNotEmpty(tempProperty = config.getProperty(R_VIRTUALHOST_DEFAULT))) {
            VIRTUALHOST_DEFAULT = tempProperty;
        }
        if (StringUtils.isNotEmpty(tempProperty = config.getProperty(R_VIRTUALHOSTS))) {
            VIRTUALHOSTS = tempProperty;
        }
        if (StringUtils.isNotEmpty(tempProperty = config.getProperty(R_THREAD_POOL))) {
            THREAD_POOL = tempProperty;
        }
        POOL_NUM = CONNECTION_NUM * CHANNEL_PER_CONNECTION;
    }
}
